﻿hahaha1 package caro_game;
//ajkdhgjksahg;ahsfkasjfklaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioSystem;

public class Server extends javax.swing.JFrame {

    ServerSocket server_socket;
    DataInputStream NhanTuCL;
    Socket socket;
    boolean co = true;
    boolean co1 = true;

    static int kt = 0;
    //Tạo luồng gửi dl đến Server.
    DataOutputStream guidenCL;
    JButton[][] bt = new JButton[20][20];
    boolean flag = true;
    String ten = "";
    int cong;
    String ip;
    String TenMayChu,IpMayChu;
    int Thang=0;
    int Thua=0;

    public Server() {
        initComponents();
    }

    public Server(int c, String ipp) {
        initComponents();
        this.cong = c;
        this.ip = ipp;

    }

    public void banco() {
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                bt[i][j] = new JButton();
                bt[i][j].setSize(35, 35);
                bt[i][j].setLocation(j * 30, i * 30);
                bt[i][j].setText("");
                bt[i][j].setBackground(Color.PINK);
                bt[i][j].setName("A" + i + "," + j);
                bt[i][j].addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        NutBanCoActionPerformed(evt);
                    }
                });
                pnBanCo.add(bt[i][j]);
            }
        }
    }

    private void NutBanCoActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        JButton button = (JButton) evt.getSource();
        if ((flag == true && co1 == false) && (button.getBackground() != Color.GRAY) && (button.getBackground() != Color.CYAN)) {
            button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/x.png")));
            flag = !flag;
            button.setBackground(Color.CYAN);
            ten = button.getName();

            try {
                guidenCL = new DataOutputStream((socket.getOutputStream()));
                //gui ten di.
                guidenCL.writeBytes(button.getName() + "\n");

            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Lỗi.!");
            }
            kiemtra();
        }
    }
    public void kiemtra() {
        int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0;
        // kiem tra chieu ngang
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                if (bt[i][j].getName().equals(ten)) {
                    for (int k = j + 1; k < 20; k++) {
                        if (bt[i][k].getBackground().equals(Color.CYAN)) {
                            x1++;
                        } else {
                            break;
                        }
                    }
                    for (int l = j - 1; l >= 0; l--) {
                        if (bt[i][l].getBackground().equals(Color.CYAN)) {
                            x1++;
                        } else {
                            break;
                        }
                    }
                    if (x1 >= 4) {
                        for (int k = j + 1; k < 20; k++) {
                            if (bt[i][k].getBackground().equals(Color.CYAN)) {
                                bt[i][k].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                            } else {
                                break;
                            }
                        }
                        for (int l = j - 1; l >= 0; l--) {
                            if (bt[i][l].getBackground().equals(Color.CYAN)) {
                                bt[i][l].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                            } else {
                                break;
                            }
                        }   
                        thongbao();
                    }
                }
            }
        }
        //kiem tra chieu doc
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                if (bt[i][j].getName().equals(ten)) {
                    for (int k = i + 1; k < 20; k++) {
                        if (bt[k][j].getBackground().equals(Color.CYAN)) {
                            x2++;
                        } else {
                            break;
                        }
                    }
                    for (int l = i - 1; l >= 0; l--) {
                        if (bt[l][j].getBackground().equals(Color.CYAN)) {
                            x2++;
                        } else {
                            break;
                        }
                    }
                    if (x2 >= 4) {
                        for (int k = i + 1; k < 20; k++) {
                            if (bt[k][j].getBackground().equals(Color.CYAN)) {
                                bt[k][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                            } else {
                                break;
                            }
                        }
                        for (int l = i - 1; l >= 0; l--) {
                            if (bt[l][j].getBackground().equals(Color.CYAN)) {
                                bt[l][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                            } else {
                                break;
                            }
                        }
                        thongbao();
                    }
                }
            }
        }
        //kiem tra theo duong cheo chính
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                if (bt[i][j].getName().equals(ten)) {
                    for (int k = 1; k <= 20; k++) {
                        int a = i + k;
                        int b = j + k;
                        if (a <= 19 & b <= 19) {
                            if (bt[i + k][j + k].getBackground().equals(Color.CYAN)) {
                                x3++;
                            } else {
                                break;
                            }
                        }
                    }
                    for (int k = 1; k <= 20; k++) {
                        int a = i - k;
                        int b = j - k;
                        if (a >= 0 & b >= 0) {
                            if (bt[i - k][j - k].getBackground().equals(Color.CYAN)) {
                                x3++;
                            } else {
                                break;
                            }
                        }
                    }
                    if (x3 >= 4) {
                        for (int k = 1; k <= 20; k++) {
                            int a = i + k;
                            int b = j + k;
                            if (a <= 19 & b <= 19) {
                                if (bt[i + k][j + k].getBackground().equals(Color.CYAN)) {
                                    bt[i + k][j + k].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                    bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                } else {
                                    break;
                                }
                            }
                        }
                        for (int k = 1; k <= 20; k++) {
                            int a = i - k;
                            int b = j - k;
                            if (a >= 0 & b >= 0) {
                                if (bt[i - k][j - k].getBackground().equals(Color.CYAN)) {
                                    bt[i - k][j - k].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                    bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                } else {
                                    break;
                                }
                            }
                        }
                        thongbao();
                    }
                }
            }
        }
        //kiem tra theo duong cheo phu
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                if (bt[i][j].getName().equals(ten)) {
                    for (int k = 1; k <= 20; k++) {
                        int a = i + k;
                        int b = j - k;
                        if (a <= 19 & b >= 0) {
                            if (bt[i + k][j - k].getBackground().equals(Color.CYAN)) {
                                x4++;
                            } else {
                                break;
                            }
                        }
                    }
                    for (int k = 1; k <= 20; k++) {
                        int a = i - k;
                        int b = j + k;
                        if (a >= 0 & b <= 19) {
                            if (bt[i - k][j + k].getBackground().equals(Color.CYAN)) {
                                x4++;
                            } else {
                                break;
                            }
                        }
                    }
                    if (x4 >= 4) {
                        for (int k = 1; k <= 20; k++) {
                            int a = i + k;
                            int b = j - k;
                            if (a <= 19 & b >= 0) {
                                if (bt[i + k][j - k].getBackground().equals(Color.CYAN)) {
                                    bt[i + k][j - k].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                    bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                } else {
                                    break;
                                }
                            }
                        }
                        for (int k = 1; k <= 20; k++) {
                            int a = i - k;
                            int b = j + k;
                            if (a >= 0 & b <= 19) {
                                if (bt[i - k][j + k].getBackground().equals(Color.CYAN)) {
                                    bt[i - k][j + k].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                    bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                } else {
                                    break;
                                }
                            }
                        }
                        thongbao();
                    }
                }
            }
        }
    }

    public void thongbao() {
        Thang++;
        lbban.setText(Thang+"");
        try {
            guidenCL = new DataOutputStream((socket.getOutputStream()));
            //gui ten di.
            guidenCL.writeBytes("`````````" + "\n");

        } catch (IOException ex) {
//            JOptionPane.showMessageDialog(null, "Lỗi");
        }
        int kq = JOptionPane.showConfirmDialog(null, "Bạn đã thắng! Bạn có muốn chơi tiếp không?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (kq != 0) {
            System.exit(kq);
        } else {
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 20; j++) {
                    bt[i][j].setBackground(Color.PINK);
                    bt[i][j].setIcon(null);
                    flag = true;
                }
            }
        }
    }

    public void KetNoiClient() {
        lbhhhhhh.setText("Đang chờ đối thủ kết nối đến...");
        try {
            server_socket = new ServerSocket(cong);
            socket = server_socket.accept();
            //Tạo luồng nhận dl từ client.
            NhanTuCL = new DataInputStream(socket.getInputStream());
            co = false;
            co1 = false;
            JOptionPane.showMessageDialog(null, "Bắt đầu chơi!");
//            int tb = JOptionPane.showConfirmDialog(null, "Bắt đầu chơi?", "Thông báo", JOptionPane.YES_NO_OPTION);
            InetAddress addr = InetAddress.getLocalHost();
            //Host IP Address
            TenMayChu = addr.getHostName();
            
            //Lay ten cua may khach
            String TenMayKhach = socket.getInetAddress().getHostName();
//            System.out.println("Tên máy khách là: "+TenMayKhach);
            
            while (true) {
                lbhhhhhh.setText("Đang chơi!");
                try {
                    int d = 0;
//                    //Đọc dl từ Client gửi lại
                    String doc = NhanTuCL.readLine();
                    
                    if (doc.equals("`````````")) {
                        Thua++;
                        lbThua.setText(Thua+"");
                        int kq = JOptionPane.showConfirmDialog(null, "Bạn thua rồi! Bạn có muốn chơi tiếp không?", "Thông báo", JOptionPane.YES_NO_OPTION);
                        if (kq != 0) {
                            System.exit(kq);

                        } else {
                            for (int i = 0; i < 20; i++) {
                                for (int j = 0; j < 20; j++) {
                                    bt[i][j].setBackground(Color.PINK);
                                    bt[i][j].setIcon(null);
                                    flag = false;

                                }
                            }
                        }
                    } else if (doc.equals("CHOILAIKKK?")) {
                        guidenCL = new DataOutputStream(socket.getOutputStream());
                        int kq = JOptionPane.showConfirmDialog(null, "Đối thủ muốn chơi lại, bạn có đồng ý không?", "Thông báo", JOptionPane.YES_NO_OPTION);
                        if (kq == 0) {
                            guidenCL.writeBytes("COKKK" + "\n");
                            for (int i = 0; i < 20; i++) {
                                for (int j = 0; j < 20; j++) {
                                    bt[i][j].setBackground(Color.PINK);
                                    bt[i][j].setIcon(null);
                                    flag = true;
                                }
                            }
                        } else {
                            guidenCL.writeBytes("KOKKK" + "\n");
                        }
                    } else if (doc.equals("XINTHUAKKK?")) {
                        guidenCL = new DataOutputStream(socket.getOutputStream());
                        int kq = JOptionPane.showConfirmDialog(null, "Đối thủ đầu hàng, bạn có đồng ý không?", "Thông báo", JOptionPane.YES_NO_OPTION);
                        if (kq == 0) {
                            Thang++;
                            lbban.setText(Thang+"");
                            guidenCL.writeBytes("CHOPHEPKKK" + "\n");
                            if(JOptionPane.showConfirmDialog(null, "Bạn có muốn chơi tiếp không?", "Thông báo", JOptionPane.YES_NO_OPTION) == 0) {
                                for (int i = 0; i < 20; i++) {
                                    for (int j = 0; j < 20; j++) {
                                        bt[i][j].setBackground(Color.PINK);
                                        bt[i][j].setIcon(null);
                                        flag = true;
                                    }
                                }
                            }
                            else
                                System.exit(0);
                        } else {
                            guidenCL.writeBytes("KOCHOPHEPKKK" + "\n");
                        }
                    } else if (doc.equals("XINHOAKKK?")) {
                        guidenCL = new DataOutputStream(socket.getOutputStream());
                        int kq = JOptionPane.showConfirmDialog(null, "Đối thủ xin hòa, bạn có đồng ý không?", "Thông báo", JOptionPane.YES_NO_OPTION);
                        if (kq == 0) {
                            guidenCL.writeBytes("CHOPHEPHOAKKK" + "\n");
                            for (int i = 0; i < 20; i++) {
                                for (int j = 0; j < 20; j++) {
                                    bt[i][j].setBackground(Color.PINK);
                                    bt[i][j].setIcon(null);
                                    flag = true;
                                }
                            }
                        } else {
                            guidenCL.writeBytes("KOCHOPHEPHOAKKK" + "\n");
                        }
                    }
                    else if (doc.equals("COKKK")) {
                        for (int i = 0; i < 20; i++) {
                            for (int j = 0; j < 20; j++) {
                                bt[i][j].setBackground(Color.PINK);
                                bt[i][j].setIcon(null);
                                flag = false;
                            }
                        }
                    } else if (doc.equals("CHOPHEPKKK")) {
                        Thua++;
                        lbThua.setText(Thua+"");
                        int kq =JOptionPane.showConfirmDialog(null, "Bạn có muốn chơi tiếp không?", "Thông báo", JOptionPane.YES_NO_OPTION);
                        if (kq == 0) {
                            for (int i = 0; i < 20; i++) {
                                for (int j = 0; j < 20; j++) {
                                    bt[i][j].setBackground(Color.PINK);
                                    bt[i][j].setIcon(null);
                                    flag = false;
                                }
                            }
                        }
                        else
                        System.exit(0);
                        
                    } else if (doc.equals("CHOPHEPHOAKKK")) {
                        for (int i = 0; i < 20; i++) {
                            for (int j = 0; j < 20; j++) {
                                bt[i][j].setBackground(Color.PINK);
                                bt[i][j].setIcon(null);
                                flag = false;
                            }
                        }
                    } else if (doc.equals("KOKKK") || doc.equals("KOCHOPHEPKKK") || doc.equals("KOCHOPHEPHOAKKK")) {
                    } else {
                        if (doc.equals("``````````")) {
                            int kq = JOptionPane.showConfirmDialog(null, "Hòa rồi! Bạn có muốn chơi tiếp không?", "Thông báo", JOptionPane.YES_NO_OPTION);
                            if (kq != 0) {
                                System.exit(kq);

                            } else {
                                for (int i = 0; i < 20; i++) {
                                    for (int j = 0; j < 20; j++) {
                                        bt[i][j].setBackground(Color.PINK);
                                        bt[i][j].setIcon(null);
                                        flag = true;
                                    }
                                }
                            }
                        } else {
                            for (int i = 0; i < 20; i++) {
                                for (int j = 0; j < 20; j++) {
                                    String c = "A" + i + "," + j;
                                    if (doc.equals(c)) {
                                        d++;
                                    }
                                }
                            }
                            if (d == 0) {
                                String doc1 = NhanTuCL.readUTF();
                                txtHienThi.append(TenMayKhach + ": " + doc1 + "\n");
                            } else {
                                for (int i = 0; i < 20; i++) {
                                    for (int j = 0; j < 20; j++) {
                                        if (bt[i][j].getName().equals(doc)) {
                                            bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/o.png")));
                                            flag = true;
                                            bt[i][j].setBackground(Color.gray);
                                            //Ngang
                                            int x1=0;
                                            for (int k = j + 1; k < 20; k++) {
                                                if (bt[i][k].getBackground().equals(Color.gray)) {
                                                    x1++;
                                                } else {
                                                    break;
                                                }
                                            }
                                            for (int l = j - 1; l >= 0; l--) {
                                                if (bt[i][l].getBackground().equals(Color.gray)) {
                                                    x1++;
                                                } else {
                                                    break;
                                                }
                                            }
                                            if (x1 >= 4) {
                                                for (int k = j + 1; k < 20; k++) {
                                                    if (bt[i][k].getBackground().equals(Color.gray)) {
                                                        bt[i][k].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                        bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                    } else {
                                                        break;
                                                    }
                                                }
                                                for (int l = j - 1; l >= 0; l--) {
                                                    if (bt[i][l].getBackground().equals(Color.gray)) {
                                                        bt[i][l].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                        bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                    } else {
                                                        break;
                                                    }
                                                }
                                            }
                                            //Dọc
                                            int x2=0;
                                            for (int k = i + 1; k < 20; k++) {
                                                if (bt[k][j].getBackground().equals(Color.gray)) {
                                                    x2++;
                                                } else {
                                                    break;
                                                }
                                            }
                                            for (int l = i - 1; l >= 0; l--) {
                                                if (bt[l][j].getBackground().equals(Color.gray)) {
                                                    x2++;
                                                } else {
                                                    break;
                                                }
                                            }
                                            if (x2 >= 4) {
                                                for (int k = i + 1; k < 20; k++) {
                                                    if (bt[k][j].getBackground().equals(Color.gray)) {
                                                        bt[k][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                        bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                    } else {
                                                        break;
                                                    }
                                                }
                                                for (int l = i - 1; l >= 0; l--) {
                                                    if (bt[l][j].getBackground().equals(Color.gray)) {
                                                        bt[l][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                        bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                    } else {
                                                        break;
                                                    }
                                                }
                                            }
                                            //Chéo chính
                                            int x3=0;
                                            for (int k = 1; k <= 20; k++) {
                                                int a = i + k;
                                                int b = j + k;
                                                if (a <= 19 & b <= 19) {
                                                    if (bt[i + k][j + k].getBackground().equals(Color.gray)) {
                                                        x3++;
                                                    } else {
                                                        break;
                                                    }
                                                }
                                            }
                                            for (int k = 1; k <= 20; k++) {
                                                int a = i - k;
                                                int b = j - k;
                                                if (a >= 0 & b >= 0) {
                                                    if (bt[i - k][j - k].getBackground().equals(Color.gray)) {
                                                        x3++;
                                                    } else {
                                                        break;
                                                    }
                                                }
                                            }
                                            if (x3 >= 4) {
                                                for (int k = 1; k <= 20; k++) {
                                                    int a = i + k;
                                                    int b = j + k;
                                                    if (a <= 19 & b <= 19) {
                                                        if (bt[i + k][j + k].getBackground().equals(Color.gray)) {
                                                            bt[i + k][j + k].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                            bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                        } else {
                                                            break;
                                                        }
                                                    }
                                                }
                                                for (int k = 1; k <= 20; k++) {
                                                    int a = i - k;
                                                    int b = j - k;
                                                    if (a >= 0 & b >= 0) {
                                                        if (bt[i - k][j - k].getBackground().equals(Color.gray)) {
                                                            bt[i - k][j - k].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                            bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                        } else {
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            //Chéo phụ
                                            int x4 = 0;
                                            for (int k = 1; k <= 20; k++) {
                                                int a = i + k;
                                                int b = j - k;
                                                if (a <= 19 & b >= 0) {
                                                    if (bt[i + k][j - k].getBackground().equals(Color.gray)) {
                                                        x4++;
                                                    } else {
                                                        break;
                                                    }
                                                }
                                            }
                                            for (int k = 1; k <= 20; k++) {
                                                int a = i - k;
                                                int b = j + k;
                                                if (a >= 0 & b <= 19) {
                                                    if (bt[i - k][j + k].getBackground().equals(Color.gray)) {
                                                        x4++;
                                                    } else {
                                                        break;
                                                    }
                                                }
                                            }
                                            if (x4 >= 4) {
                                                for (int k = 1; k <= 20; k++) {
                                                    int a = i + k;
                                                    int b = j - k;
                                                    if (a <= 19 & b >= 0) {
                                                        if (bt[i + k][j - k].getBackground().equals(Color.gray)) {
                                                            bt[i + k][j - k].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                            bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                        } else {
                                                            break;
                                                        }
                                                    }
                                                }
                                                for (int k = 1; k <= 20; k++) {
                                                    int a = i - k;
                                                    int b = j + k;
                                                    if (a >= 0 & b <= 19) {
                                                        if (bt[i - k][j + k].getBackground().equals(Color.gray)) {
                                                            bt[i - k][j + k].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                            bt[i][j].setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/skull.png")));
                                                        } else {
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Đối thủ đã thoát!");
                    System.exit(cong);
                }
            }
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Đối thủ đã thoát!");
            System.exit(cong);
        }
    }

    public void gui() {
        if (co1 == false && txtChat.getText().length()!=0) {
            txtHienThi.append(TenMayChu+": " + txtChat.getText() + "\n");
            String txtchat = txtChat.getText();
            txtChat.setText("");
            try {
                //Tạo luồng gửi dl đến Server.
                guidenCL = new DataOutputStream(socket.getOutputStream());
                //Gui cho server
                guidenCL.writeBytes(txtchat + "\n");
                guidenCL.writeUTF(txtchat);
                
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Lỗi rồi nhé!\n" + ex);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnBanCo = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtHienThi = new javax.swing.JTextArea();
        txtChat = new javax.swing.JTextField();
        jButton7 = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        btnCauHoa = new javax.swing.JButton();
        btnXinThua = new javax.swing.JButton();
        btnChoiLai = new javax.swing.JButton();
        lbhhhhhh = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lbban = new javax.swing.JLabel();
        lbThua = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 51, 0));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        pnBanCo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        pnBanCo.setFocusCycleRoot(true);
        pnBanCo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pnBanCoMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout pnBanCoLayout = new javax.swing.GroupLayout(pnBanCo);
        pnBanCo.setLayout(pnBanCoLayout);
        pnBanCoLayout.setHorizontalGroup(
            pnBanCoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );
        pnBanCoLayout.setVerticalGroup(
            pnBanCoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 601, Short.MAX_VALUE)
        );

        txtHienThi.setEditable(false);
        txtHienThi.setColumns(20);
        txtHienThi.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        txtHienThi.setRows(5);
        jScrollPane1.setViewportView(txtHienThi);

        txtChat.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtChatKeyPressed(evt);
            }
        });

        jButton7.setBackground(new java.awt.Color(204, 204, 0));
        jButton7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton7.setForeground(new java.awt.Color(204, 0, 0));
        jButton7.setText("Gửi");
        jButton7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton7MouseClicked(evt);
            }
        });
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        btnThoat.setBackground(new java.awt.Color(204, 204, 0));
        btnThoat.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnThoat.setForeground(new java.awt.Color(204, 0, 0));
        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnCauHoa.setBackground(new java.awt.Color(204, 204, 0));
        btnCauHoa.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnCauHoa.setForeground(new java.awt.Color(204, 0, 0));
        btnCauHoa.setText("Cầu hòa");
        btnCauHoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCauHoaActionPerformed(evt);
            }
        });

        btnXinThua.setBackground(new java.awt.Color(204, 204, 0));
        btnXinThua.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnXinThua.setForeground(new java.awt.Color(204, 0, 0));
        btnXinThua.setText("Xin thua");
        btnXinThua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXinThuaActionPerformed(evt);
            }
        });

        btnChoiLai.setBackground(new java.awt.Color(204, 204, 0));
        btnChoiLai.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnChoiLai.setForeground(new java.awt.Color(204, 0, 0));
        btnChoiLai.setText("Chơi lại");
        btnChoiLai.setName("btchoilai"); // NOI18N
        btnChoiLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChoiLaiActionPerformed(evt);
            }
        });

        lbhhhhhh.setText("   ");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("BẠN");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setText("ĐỐI THỦ");

        lbban.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lbban.setText("0");

        lbThua.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lbThua.setText("0");

        jLabel3.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N
        jLabel3.setText("Tỉ số:");

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/caro_game/anh.png"))); // NOI18N

        jMenu1.setText("Tùy chọn      ");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem1.setText("Thoát");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Hướng dẫn");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnBanCo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btnChoiLai, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(69, 69, 69)
                        .addComponent(btnXinThua, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(70, 70, 70)
                        .addComponent(btnCauHoa, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(lbban)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(60, 60, 60)
                                .addComponent(jLabel2)
                                .addGap(0, 45, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lbThua)
                                .addGap(76, 76, 76))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jScrollPane1)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lbhhhhhh)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtChat)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(90, 90, 90)
                                .addComponent(jLabel3)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(pnBanCo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbThua)
                            .addComponent(lbban))
                        .addGap(26, 26, 26)
                        .addComponent(lbhhhhhh, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtChat, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnChoiLai, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnXinThua, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnCauHoa, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtChatKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtChatKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == evt.VK_ENTER) {
            gui();
        }
    }//GEN-LAST:event_txtChatKeyPressed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        int kq = JOptionPane.showConfirmDialog(null, "Bán muốn thoát chương trình?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (kq == 0) {
            if (co == true) {
                try {
                    server_socket.close();
                    Socket s = new Socket(ip, 123);

                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Lỗi gửi thông báo cho máy chủ.");
                }
            }
            System.exit(kq);
        }
    }//GEN-LAST:event_btnThoatActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        gui();
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton7MouseClicked
        // TODO add your handling code here:
        gui();
    }//GEN-LAST:event_jButton7MouseClicked

    private void pnBanCoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnBanCoMouseClicked
        // TODO add your handling code here:

    }//GEN-LAST:event_pnBanCoMouseClicked

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        int kq = JOptionPane.showConfirmDialog(null, "Bán muốn thoát chương trình?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (kq == 0) {
            if (co == true) {
                try {
                    server_socket.close();
                    Socket s = new Socket(ip, 123);

                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Lỗi gửi thông báo cho máy chủ.");
                }
            }
            System.exit(kq);
        } else {
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        int kq = JOptionPane.showConfirmDialog(null, "Bán muốn thoát chương trình?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (kq == 0) {
            if (co == true) {
                try {
                    server_socket.close();
                    Socket s = new Socket(ip, 123);

                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Lỗi gửi thông báo cho máy chủ.");
                }
            }
            System.exit(kq);
        } else {
        }
    }//GEN-LAST:event_formWindowClosing

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosed

    private void btnChoiLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChoiLaiActionPerformed
        // TODO add your handling code here:
        int kq = JOptionPane.showConfirmDialog(null, "Bạn muốn chơi lại?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (kq == 0) {
            try {
                guidenCL = new DataOutputStream(socket.getOutputStream());
                guidenCL.writeBytes("CHOILAIKKK?" + "\n");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Lỗi gửi thông báo chơi lại.");
            }
        }
    }//GEN-LAST:event_btnChoiLaiActionPerformed

    private void btnXinThuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXinThuaActionPerformed
        // TODO add your handling code here:
        int kq = JOptionPane.showConfirmDialog(null, "Bạn muốn đầu hàng?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (kq == 0) {
            try {
                guidenCL = new DataOutputStream(socket.getOutputStream());
                guidenCL.writeBytes("XINTHUAKKK?" + "\n");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Lỗi gửi thông báo đầu hàng.");
            }
        }
    }//GEN-LAST:event_btnXinThuaActionPerformed

    private void btnCauHoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCauHoaActionPerformed
        // TODO add your handling code here:
        int kq = JOptionPane.showConfirmDialog(null, "Bạn muốn xin hòa?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (kq == 0) {
            try {
                guidenCL = new DataOutputStream(socket.getOutputStream());
                guidenCL.writeBytes("XINHOAKKK?" + "\n");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Lỗi gửi thông báo cầu hòa.");
            }
        }
    }//GEN-LAST:event_btnCauHoaActionPerformed

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//                new Server().setVisible(true);
            }
        });
        Server s = new Server();
        s.setTitle("Server");
        s.banco();
        s.setVisible(true);
        s.KetNoiClient();

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCauHoa;
    private javax.swing.JButton btnChoiLai;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXinThua;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbThua;
    private javax.swing.JLabel lbban;
    private javax.swing.JLabel lbhhhhhh;
    private javax.swing.JPanel pnBanCo;
    private javax.swing.JTextField txtChat;
    private javax.swing.JTextArea txtHienThi;
    // End of variables declaration//GEN-END:variables
}
